﻿using System;
using System.Collections.Generic;

namespace UnityModule
{
    public class SpawnItemScheduler : SingletonBehaviour<SpawnItemScheduler>
    {
        public static int m_MaxProcessCountLimit = 3;
        private Queue<SpawnPrefabInfo> m_RequestPrefabQueue = new Queue<SpawnPrefabInfo>();

        public void Spawn(SpawnPrefabInfo spInfo)
        {
            if (spInfo.Priority == RequestPriority.High){
                spInfo.DoCallBack();
                ReferencePoolManager.Release(spInfo);
            }
            else{
                m_RequestPrefabQueue.Enqueue(spInfo);
            }
        }

        private void Update()
        {
            int count = m_RequestPrefabQueue.Count;
            if (count > 0){
                int processCount = Math.Max(m_MaxProcessCountLimit, count / 10);
                processCount = Math.Min(count, processCount);
                for (int i = 0; i < processCount; ++i){
                    SpawnPrefabInfo info = m_RequestPrefabQueue.Dequeue();
                    info.DoCallBack();
                    ReferencePoolManager.Release(info);
                }
            }
        }

        public enum RequestPriority
        {
            Low,
            High
        }
    }
}

